﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.MenuCommand;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.MenuCommandHandler
{
    public class MenuUpdateCommandHandler : IRequestHandler<MenuUpdateCommand, APIResult<string>>
    {
        private readonly IBaseRepository<MenuModel> menuRepos;

        public MenuUpdateCommandHandler(IBaseRepository<MenuModel> menuRepos)
        {
            this.menuRepos = menuRepos;
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<string>> Handle(MenuUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            var obj = menuRepos.GetAll().Result.FirstOrDefault(x => x.MenuId == request.MenuId);
            obj.MenuName = request.MenuName;
            obj.ParentId = request.ParentId;
            obj.Url = request.Url;
            obj.UpdateName = request.UpdateName;
            obj.UpdateTime = DateTime.Now;
            int res = menuRepos.Update(obj).Result;
            if (res > 0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = res.ToString();
            }else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
            }

            return await Task.FromResult(result);
        }
    }
}
